Java class file
part 6/9 · 24.1 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Representation of a class file
The following is a representation of a .class file as if it were a C-style struct.
struct ClassFileFormat {
u4 magicNumber;
u2 minorVersion;
u2 majorVersion;
u2 constantPoolCount;
ConstantPoolInfo[constantPoolCount - 1] constantPool;
u2 accessFlags;
u2 thisClass;
u2 superClass;
u2 interfacesCount;
u2[interfacesCount] interfaces;
u2 fieldsCount;
FieldInfo[fieldsCount] fields;
u2 methodsCount;
MethodInfo[methodsCount] methods;
u2 attributesCount;
AttributeInfo[attributesCount] attributes;
}
The constant pool
The constant pool table is where most of the literal constant values are stored. This includes values such as numbers of all sorts, strings, identifier names, references to classes and methods, and type descriptors. All indexes, or references, to specific constants in the constant pool table are given by 16-bit (type u2) numbers, where index value 1 refers to the first constant in the table (index value 0 is invalid).
Due to historic choices made during the file format development, the number of constants in the constant pool table is not actually the same as the constant pool count which precedes the table. First, the table is indexed starting at 1 (rather than 0), but the count should actually be interpreted as the maximum index plus one.cite-ref-jvms-4-4-6-0[6] Additionally, two types of constants (longs and doubles) take up two consecutive slots in the table, although the second such slot is a phantom index that is never directly used.
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────